//
//  DDMyGift.m
//  dingdang
//
//  Created by lichq on 7/24/15.
//  Copyright (c) 2015 阮 沧晖. All rights reserved.
//

#import "DDMyGift.h"
#import "DDMGiftOrder.h"
#import "UICommonFunc.h"
#import "DDNetWorkAPIClient.h"
#import "UIImageView+AFNetworking.h"
#import "DDMyGiftDetail.h"
#import "UIViewController+MJPopupViewController.h"

@interface DDMyGift (){
    NSMutableArray *lists;
}
@property(nonatomic, strong) IBOutlet UICollectionView *cv;


@end

@implementation DDMyGift

- (void)viewWillAppear:(BOOL)animated
{
    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
    [network queryGitfOrder:^(BOOL isSuccess, NSDictionary *dict, NSString *message, NSString *status) {
        NSLog(@"%@", dict);
        NSError *error;
        DDMGiftOrder *log = [[DDMGiftOrder alloc] initWithDictionary:dict error:&error];
        if (error) {
            NSLog(@"%@", error.userInfo);
        }
        
        lists = [[NSMutableArray alloc]init];
        [lists removeAllObjects];
        if (log.list.count == 0) {
            [UICommonFunc showAlert:@"您还没有礼品哦，赶快去兑换礼品吧!"];
            return ;
        }
        for (int i=0; i<log.list.count; i++) {
            DDMGiftOrderCell1 *cell = log.list[i];
            [lists addObject:cell];
        }
        
        if (isSuccess) {
            //            _intgeral = [[dict objectForKey:@"integral"]stringValue];
            //            _intgeralLabel.text =[NSString stringWithFormat:@"我累积的积分：%@",_intgeral];
        }
        [self.cv reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    
    UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil];
    [self.cv registerNib:cellNib forCellWithReuseIdentifier:@"IND_cvCell"];
    //    [self.cv setCollectionViewLayout:[self getFlowLayout]];
    [self.cv setBackgroundColor:[UIColor clearColor]];
}


- (void)createUI
{
    self.view.backgroundColor = RGB(245, 245, 245);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:NSLocalizedString(@"我的礼品", nil) parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    [self AddLeftBtnAction:nil normal:@"top_返回" selected:@"top_返回" action:^{
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    return [lists count];
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(([UIScreen mainScreen].bounds.size.width-50)/3, 126);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView  cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *cellIdentifier = @"IND_cvCell";
    DDMGiftOrderCell1 *info = [lists objectAtIndex:indexPath.row];
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    UIImageView *imageV = (UIImageView *)[cell viewWithTag:10];
    [imageV setImageWithURL:[NSURL URLWithString:info.imgPath]];
    imageV.contentMode = UIViewContentModeScaleAspectFit;
    UILabel *labName = (UILabel *)[cell viewWithTag:11];
    labName.text = info.name;
    UILabel *labPrice = (UILabel *)[cell viewWithTag:12];
    labPrice.text = info.price;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DDMGiftOrderCell1 *info = [lists objectAtIndex:indexPath.row];
    DDMyGiftDetail *myGiftDetail = [[DDMyGiftDetail alloc] initWithNibName:@"DDMyGiftDetail" bundle:nil];
    myGiftDetail.info = info;
    myGiftDetail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:myGiftDetail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
